package com.test.slice;

import com.ms.banner.Banner;
import com.ms.banner.BannerConfig;
import com.test.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.PageSlider;

import java.util.ArrayList;
import java.util.List;

import static com.test.MyApplication.getDataInt;

public class CustomViewPagerAbilitySlice extends AbilitySlice {

    private Banner banner1;
    private Banner banner2;
    private Banner banner3;
    private Banner banner4;
    private DirectionalLayout indicator;
    private List<Image> indicatorImages = new ArrayList<>();
    private int mIndicatorSelectedResId = ResourceTable.Media_indicator;
    private int mIndicatorUnselectedResId = ResourceTable.Media_indicator2;
    private int lastPosition = 0;
    private List<String> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_custom_view_pager_aibility);

        banner1 = (Banner) findComponentById(ResourceTable.Id_banner1);
        banner2 = (Banner) findComponentById(ResourceTable.Id_banner2);
        banner3 = (Banner) findComponentById(ResourceTable.Id_banner3);
        banner4 = (Banner) findComponentById(ResourceTable.Id_banner4);
        indicator = (DirectionalLayout) findComponentById(ResourceTable.Id_indicator);
        indicatorImages.clear();

        mList = new ArrayList<>();
        mList.add("https://upload-images.jianshu.io/upload_images/9913211-8f28da27dd550fe5.jpg");
        mList.add("https://upload-images.jianshu.io/upload_images/9913211-a0575ae640238bfe.jpg");

        initIndicator();

        banner1.setAutoPlay(true)
                .setPages(mList, new CustomViewHolder())
                .setBannerStyle(BannerConfig.NOT_INDICATOR)
                .start();

        banner1.setOnPageChangeListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int position) {
                indicatorImages.get((lastPosition + mList.size()) % mList.size()).setImageAndDecodeBounds(mIndicatorUnselectedResId);
                indicatorImages.get((position + mList.size()) % mList.size()).setImageAndDecodeBounds(mIndicatorSelectedResId);
                lastPosition = position;
            }
        });

        banner2.setAutoPlay(true)
                .setPages(getDataInt(), new CustomViewHolder())
                .start();

        banner3.setAutoPlay(true)
                .setPages(getDataInt(), new CustomViewHolder())
                .start();

        banner4.setAutoPlay(true)
                .setPages(getDataInt(), new CustomViewHolder())
                .start();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    private void initIndicator() {
        for (int i = 0; i < mList.size(); i++) {
            Image imageView = new Image(this);
            imageView.setScaleMode(Image.ScaleMode.CLIP_CENTER);
            DirectionalLayout.LayoutConfig custom_params = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_CONTENT,
                    DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            custom_params.setMarginLeft(2);
            custom_params.setMarginRight(2);
            if (i == 0) {
                imageView.setImageAndDecodeBounds(mIndicatorSelectedResId);
            } else {
                imageView.setImageAndDecodeBounds(mIndicatorUnselectedResId);
            }
            indicatorImages.add(imageView);
            indicator.addComponent(imageView, custom_params);
        }
    }

    //如果你需要考虑更好的体验，可以这么操作
    @Override
    protected void onStop() {
        super.onStop();
        //结束轮播
        banner1.stopAutoPlay();
        banner2.stopAutoPlay();
        banner3.stopAutoPlay();
        banner4.stopAutoPlay();
    }
}
